 
from loguru import logger
from tools import utils
from tools.util_statck import todegree
from astropy.io import fits
import numpy as np
if __name__ == "__main__":
        
    ra_="2:28:16.5000"
    dec_="19:11:39.900"
    ra_dec = todegree(ra_,dec_)
    catalog_path="/mnt/7b21f1e1-eb25-4cd5-bdb5-06d7d82fa253/Temp/force_photmetry/images/1278305/mb_sc_t1278305-19695_u_20250101143327_209_sciimg_sexcat_CalMag.fits"
    catalog = fits.getdata(catalog_path, ext=2)

    utils.CatalogueRegion(catalog_path)
    matched_star=utils.TargetXCatalogue(ra_dec[0], ra_dec[1],catalog)
    mag_zp=22.3764
    exposure=300
    if matched_star: #如果给定的位置是精确的话，但是一般应该都不够精准
        # logger.info(matched_star)
        # logger.info(f"statck_mag:{mag_zp+matched_star['MAG_PSF']}")
                    
        # convert_to_unit_time_magnitude 用于将测得的 MAG_AUTO 转换成单位时间星等
        m_per_sec, sigma_m_per_sec = utils.convert_to_unit_time_magnitude(
            matched_star['MAG_AUTO'],
            exposure,
            sigma_m=matched_star['MAGERR_AUTO']
        )
        # 进一步校正，将 Gate 零点加到单位时间星等上，得到最终校正星等
        CALMAG = mag_zp + m_per_sec
        
        # 计算单位时间星等
        flux_ = matched_star["FLUX_PSF"]
        mag_S_PAN = -2.5 * np.log10(flux_ / exposure)
        logger.info(f"mag_zp:{mag_zp},m_per_sec: {m_per_sec:.4f},CALMAG:{CALMAG},flux_s:{mag_S_PAN}  星等误差: {sigma_m_per_sec:.4f}")
        
    